package com.qingke.easyjava.xmlconverter;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class RegionInfoParser {

    public static List<Region> parseRegions(String filename){

        BufferedReader br = null;
        List<Region> results = new ArrayList<Region>();

        try {
            br = new BufferedReader(new FileReader(filename));
            String line = null;

            while ((line = br.readLine()) != null) {
                line = line.trim();
                if (line.startsWith("#")) {
                    continue;
                }

                String[] patterns = line.split(",");

                String id = patterns[0];
                String name = patterns[1];
                String aveIncome = patterns[2];
                String superRegion = patterns[3];
                String boss = (patterns.length == 5) ? patterns[4] : "";

                Region region = new Region();
                region.setId(id);
                region.setName(name);
                region.setAvgIncome(aveIncome);
                region.setSuperRegion(superRegion);
                region.setBoss(boss);

                results.add(region);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        return results;
    }
}
